package com.onehouse.infrastructure.repository.room;

import com.onehouse.domain.checkIn.adapter.repository.ICheckInRoomRepository;
import com.onehouse.domain.room.model.RoomEntity;
import com.onehouse.domain.room.model.RoomStatus;
import com.onehouse.domain.room.repository.IRoomRepository;
import com.onehouse.infrastructure.dao.RoomDao;
import com.onehouse.infrastructure.dao.po.Room;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Repository
public class RoomRepository implements IRoomRepository, ICheckInRoomRepository {
    @Resource
    private RoomDao roomDao;

    @Override
    public int insert(RoomEntity roomEntity) {
        Room room = new Room();
        room.setRoomId(roomEntity.getRoomId());
        room.setRoomNumber(roomEntity.getRoomNumber());
        room.setRoomTypeId(roomEntity.getRoomTypeId());
        room.setStatus(roomEntity.getStatus());

        return roomDao.insert(room);
    }

    @Override
    public int update(RoomEntity roomEntity) {
        Room room = new Room();
        room.setRoomId(roomEntity.getRoomId());
        room.setRoomNumber(roomEntity.getRoomNumber());
        room.setRoomTypeId(roomEntity.getRoomTypeId());
        room.setStatus(roomEntity.getStatus());

        return roomDao.update(room);
    }

    @Override
    public Optional<RoomEntity> selectByRoomNumber(String roomNumber) {
        Room room = roomDao.selectByRoomNumber(roomNumber);
        if (room == null) {
            return Optional.empty();
        }
        RoomEntity roomEntity = new RoomEntity();
        roomEntity.setRoomId(room.getRoomId());
        roomEntity.setRoomNumber(room.getRoomNumber());
        roomEntity.setRoomTypeId(room.getRoomTypeId());
        roomEntity.setStatus(room.getStatus());
        return Optional.of(roomEntity);
    }

    @Override
    public List<RoomEntity> selectList() {
        List<RoomEntity> roomEntities = new ArrayList<>();
        for (Room room : roomDao.selectList()) {
            RoomEntity roomEntity = new RoomEntity();
            roomEntity.setRoomId(room.getRoomId());
            roomEntity.setRoomNumber(room.getRoomNumber());
            roomEntity.setRoomTypeId(room.getRoomTypeId());
            roomEntity.setStatus(room.getStatus());
            roomEntities.add(roomEntity);
        }
        return roomEntities;
    }

    @Override
    public int deleteByNumber(String roomNumber) {
        return roomDao.deleteByNumber(roomNumber);
    }

    @Override
    public int deleteById(Long roomId) {
        return roomDao.deleteById(roomId);
    }

    @Override
    public boolean existedById(Long typeId) {
        return roomDao.existedById(typeId);
    }

    @Override
    public void changeRoomStatusToBooked(String roomNumber) {
        Room room = roomDao.selectByRoomNumber(roomNumber);
        if (room != null) {
            room.setStatus(RoomStatus.BOOKED);
            roomDao.update(room);
        }
    }

    @Override
    public void changeRoomStatusToAvaliable(String roomNumber) {
        Room room = roomDao.selectByRoomNumber(roomNumber);
        if (room != null) {
            room.setStatus(RoomStatus.AVALIABLE);
            roomDao.update(room);
        }
    }

    @Override
    public void changeRoomStatusToCheckedIn(String roomNumber) {
        Room room = roomDao.selectByRoomNumber(roomNumber);
        if (room != null) {
            room.setStatus(RoomStatus.CHECKED_IN);
            roomDao.update(room);
        }
    }

    @Override
    public String queryRoomStatus(String roomNumber) {
        Room room = roomDao.selectByRoomNumber(roomNumber);
        if (room == null) {
            return null;
        }
        return String.valueOf(room.getStatus());
    }
}
